﻿using System;
using System.Data;
using System.Configuration;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace ASPNETVN.PORTAL.Components.UI.DataTypes
{
    public class CheckBoxListDataType : BaseDataType
    {
        private string _DataTextField;
        private string _DataValueField;

        public CheckBoxListDataType(object dataSource, string textField, string dataField)
        {
            this._InnerDataType = PropertyDataType.List;
            this._InnerDataSource = dataSource;
            DataTextField = textField;
            DataValueField = dataField;
        }

        public override Control EditControl
        {
            get
            {
                if (this._InnerControl == null)
                {
                    // ListBox.
                    using (CheckBoxList control = new CheckBoxList())
                    {
                        control.Width = new Unit("1%");
                        control.RepeatColumns = 2;
                        control.DataSource = DataSource;
                        control.DataValueField = DataValueField;
                        control.DataTextField = DataTextField;
                        control.DataBind();
                        this._InnerControl = control;
                    }
                }

                // Update value in control.
                CheckBoxList cbl = (CheckBoxList)this._InnerControl;
                cbl.ClearSelection();
                // Store in string array.
                string[] values = this._InnerValue.Split(new char[] { ';' });
                foreach (string _value in values)
                {
                    if (cbl.Items.FindByValue(_value) != null)
                    {
                        cbl.Items.FindByValue(_value).Selected = true;
                    }
                }
                // Return control.
                return this._InnerControl;
            }
            set
            {
                if (value.GetType().Name == "CheckBoxList")
                {
                    this._InnerControl = value;

                    // Update value from control.
                    CheckBoxList cbl = (CheckBoxList)this._InnerControl;
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < cbl.Items.Count; i++)
                    {
                        if (cbl.Items[i].Selected)
                        {
                            sb.Append(cbl.Items[i].Value);
                            sb.Append(";");
                        }
                    }
                    Value = sb.ToString();
                }
                else
                    throw new ArgumentException("A CheckBoxList value is required");
            }
        }

        public override string Value
        {
            get { return (this._InnerValue); }
            set { this._InnerValue = value.TrimEnd(new char[] { ';' }); }
        }

        public string DataTextField
        {
            get { return this._DataTextField; }
            set { this._DataTextField = value; }
        }

        public string DataValueField
        {
            get { return this._DataValueField; }
            set { this._DataValueField = value; }
        }

        public override object DataSource
        {
            get { return this._InnerDataSource; }
            set { this._InnerDataSource = value; }
        }
    }
}
